package com.xbongbong.saas.factory.publicrule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.domain.entity.CustomerRuleEntity;
import com.xbongbong.saas.domain.entity.RuleCustomerEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 捞取频率限制规则功能类
 * @author : chy
 * @date 创建时间：2018-5-16 下午6:51:08
 * @version
 */
@Slf4j
@Component
public class CheckRuleGainRate implements ICheckRule {

	@Resource
	private PaasRedisHelper paasRedisHelper;

	@Override
	public boolean canGainCustomer(CustomerRuleEntity rule, String corpid,
								   UserEntity userEntity, Long customerId, int dataIdSize, JSONObject data) throws XbbException {

		//没有规则，不限制客户获取
		if(rule == null || Objects.equals(rule.getStatus(), 0)){
			return true;
		}

		//获取用户的每天的捞取、分配频次
		Integer num = StringUtil.toInt(paasRedisHelper.getValue(RedisPrefixConstant.CUSTOMER_GAIN_RATE, corpid + "_" + userEntity.getUserId()), null);
		if(num == null){
			num = getCustomerGainRateFromDB(rule, userEntity);
			if(num != null){
				Long endTime = DateTimeUtil.getTodayInt() + 86400;
                Long now = DateTimeUtil.getInt();
                long time = endTime - now;
				paasRedisHelper.setValue(RedisPrefixConstant.CUSTOMER_GAIN_RATE, corpid + "_" + userEntity.getUserId(), num, (int) time);
			}
		}
		if(num == null || dataIdSize <= num){
			return true;
		}

		return false;
	}

	@Override
	public List<RuleCustomerEntity> getBackCustomerList(UserEntity userEntity,
														CustomerRuleEntity rule, List<RuleCustomerEntity> customerList,
														Integer advanceTime) throws XbbException {
		return new ArrayList<>();
	}

	@Override
	public List<RuleCustomerEntity> getBackCustomerList(UserEntity userEntity,
			CustomerRuleEntity rule, List<RuleCustomerEntity> customerList)
			throws XbbException {
		return new ArrayList<>();
	}

	@Override
	public JSONObject customerTypeChage(CustomerRuleEntity rule, List<ItemPoJo> typeItemPoJos) throws XbbException {
		return null;
	}

	/**
     * 获取数据库中的可捞取、分配客户数
     * @param rule 公海规则捞取频率规则
     * @param userEntity 用户
     * @return
     * @throws XbbException
     * 创建时间： 2018-5-25 下午7:07:30
     * 修改时间： 2018-5-25 下午7:07:30 chy
     * @author chy
     * @since v3.22
     * @version v3.22
     */
    public Integer getCustomerGainRateFromDB(CustomerRuleEntity rule, UserEntity userEntity) throws XbbException {
        if(rule == null || Objects.equals(rule.getStatus(), 0)){
            return null;
        }

        /**
         * rule.getRuleType() 必然是 CustomerRuleEnum.GAIN_RATE_RULE.getCode()
         */
        IOperateRuleValue operateRuleValue = OperateRuleValueFactory.operateRuleValueFactory(rule.getRuleType());
        JSONArray ruleValueArray = (JSONArray) operateRuleValue.toObject(rule);

        return CheckRuleUtil.getMinValue(ruleValueArray, userEntity);
    }
}
